package com.treasure.mall.biz.service.core.withdraw.impl;

import com.treasure.mall.biz.base.AbstractSearchableCrudService;
import com.treasure.mall.biz.base.SpecificationAndSort;
import com.treasure.mall.biz.dto.BaseSaveDTO;
import com.treasure.mall.biz.dto.BaseSearchDTO;
import com.treasure.mall.biz.entity.withdraw.UserBankInfoPO;
import com.treasure.mall.biz.mapstruct.withdraw.UserBankInfoMapStruct;
import com.treasure.mall.biz.repository.withdraw.UserBankInfoRepository;
import com.treasure.mall.biz.service.core.user.UserService;
import com.treasure.mall.biz.service.core.withdraw.UserBankInfoService;
import com.treasure.mall.biz.vo.withdraw.UserBankInfoVO;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.base.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zoul
 * @version 1.0
 * @date 2020/6/14 15:41
 * @Description
 */
@Service
public class UserBankInfoServiceImpl extends AbstractSearchableCrudService<UserBankInfoPO, Long> implements UserBankInfoService {
    @Autowired
    private UserBankInfoRepository userBankInfoRepository;
    @Autowired
    private UserService userService;

    protected UserBankInfoServiceImpl(UserBankInfoRepository repository) {
        super(repository);
    }

    @Override
    protected SpecificationAndSort<UserBankInfoPO> specificationAndSort(BaseSearchDTO searchDTO) {
        return null;
    }

    @Override
    public UserBankInfoPO save(BaseSaveDTO saveDTO) {
        return null;
    }

    /**
     * 获取用户所有银行卡
     * @param userId 用户Id
     * @return
     */
    @Override
    public List<UserBankInfoVO> getUserBankList(Long userId){
        List<UserBankInfoPO> pos = userBankInfoRepository.getListByUserId(userId);
        return UserBankInfoMapStruct.INSTANCE.po2vos(pos);
    }

    /**
     * 获取用户默认银行卡
     * @param userId 用户Id
     * @return
     */
    @Override
    public UserBankInfoVO getUserDefaultBank(Long userId){
        UserBankInfoPO po = userBankInfoRepository.findFirstByUserPo_UserIdAndDeletedIsFalseOrderByDefaultFlagDesc(userId);

        return UserBankInfoMapStruct.INSTANCE.po2vo(po);
    }

    /**
     * 修改or新增银行卡信息
     * @param po 实体
     * @return
     */
    @Override
    public ApiResult addOrUpdateUserBank(UserBankInfoPO po) {
        boolean isAdd = po.getInfoId() == null || po.getInfoId() <= 0;
        if (isAdd) {
            boolean existFlag = userBankInfoRepository.existsUserBankInfo(po.getBankNo(),po.getUserPo().getUserId())>0;
            if(existFlag){
                throw new IllegalStateException("该银行卡已存在,请添加不存在的银行卡！");
            }

            int userBankNum = userBankInfoRepository.countUserBankNum(po.getUserPo().getUserId());
            if(userBankNum==0){
                userService.modifyUserRealName(po.getUserPo().getUserId(),po.getRealName());
            }
//            userBankInfoRepository.save(po);
        }
        userBankInfoRepository.saveAndFlush(po);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    /**
     * 设置默认银行卡
     * @param userId 用户Id
     * @param infoId 主键Id
     * @return
     */
    @Override
    public ApiResult setUserBankDefault(Long userId, Long infoId) {
        boolean existFlag = userBankInfoRepository.existsUserBankInfo(infoId, userId) > 0;
        if (!existFlag) {
            throw new IllegalStateException("该银行卡不属于您,请刷新后重试！");
        }
        userBankInfoRepository.delUserBankDefault(userId);
        userBankInfoRepository.setUserBankDefault(userId, infoId);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除银行卡
     *
     * @param userId
     * @param infoId
     * @return
     */
    @Override
    public void deleteUserBank(Long userId, Long infoId) {
        userBankInfoRepository.delUserBank(infoId, userId);
    }


}